﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace JCard
{
    public partial class fFamVoc : Form
    {
        #region variables
        // Parent form used when back to Parent form
        fCLesson frmParent;
        ArrayList arrVocs;
        ArrayList arrViewedVocs;
        bool isRandom = false;
        DTO_VOC curDtoVoc = null;
        int curIndex = 0;
        int viewedIndex = 0;
        int total = 0;
        string dbPath;

        DTO_Setting dtoSetting = new DTO_Setting();
        #endregion

        #region constructors
        public fFamVoc(string topicName, ArrayList arrVocs, bool isRandom, fCLesson frmParent, string dbPath)
        {
            this.arrVocs = arrVocs;
            this.isRandom = isRandom;
            this.arrViewedVocs = new ArrayList();
            this.total = this.arrVocs.Count;
            this.frmParent = frmParent;
            this.dbPath = dbPath;

            InitializeComponent();

            lblTitle.Text = topicName;
        }
        #endregion

        #region Loading & Display
        private void fFamVoc_Load(object sender, EventArgs e)
        {
            SetSettingValue();

            viewedIndex = 0;
            curIndex = -1;

            UpdateFont();
            GetVocToDisplay();
            DisplayCurVocs();

            if (total > 1)
            {
                btnNext.Enabled = true;
            }
            else
            {
                btnNext.Enabled = false;
            }

            btnPrevious.Enabled = false;
        }

        private void SetSettingValue()
        {
            // Read config
            string startupPath = Application.StartupPath;
            startupPath += @"\Setting.ini";
            dtoSetting = new DTO_Setting();
            dtoSetting = ReadIniSetting(startupPath);

            //           
            //Set config 
            //Kanji
            pnlKanji.BackColor = Color.FromArgb(dtoSetting.Kanji_BackColor);
            pnlKanji.ForeColor = Color.FromArgb(dtoSetting.Kanji_FontColor);
            lblKanji.Font = Common.ChangeFontSize(lblKanji.Font, dtoSetting.Kanji_Fontsize);

            //Hiragana
            pnlHiragana.BackColor = Color.FromArgb(dtoSetting.Hiragana_BackColor);
            pnlHiragana.ForeColor = Color.FromArgb(dtoSetting.Hiragana_FontColor);
            lblHiragana.Font = Common.ChangeFontSize(lblHiragana.Font, dtoSetting.Hiragana_Fontsize);

            //Meaning
            pnlMeaning.BackColor = Color.FromArgb(dtoSetting.Meaning_BackColor);
            pnlMeaning.ForeColor = Color.FromArgb(dtoSetting.Meaning_FontColor);
            lblMeaning.Font = Common.ChangeFontSize(lblMeaning.Font, dtoSetting.Meaning_Fontsize);
        }

        private DTO_Setting ReadIniSetting(string filePath)
        {
            BUS_Setting busSetting = new BUS_Setting();
            DTO_Setting dtoSetting = new DTO_Setting();
            dtoSetting = busSetting.ReadSetting(filePath);
            return dtoSetting;
        }

        private void DisplayCurVocs()
        {
            if (radAll.Checked)
            {
                lblKanji.Text = curDtoVoc.Kanji;
                lblHiragana.Text = curDtoVoc.Hiragana;
                lblMeaning.Text = curDtoVoc.Meaning;
            }
            else if (radKanji.Checked)
            {
                lblKanji.Text = curDtoVoc.Kanji;                
                lblHiragana.Text = Constants.RES_FAM_HIRAGANA_CLICK_VALUE;
                lblMeaning.Text = Constants.RES_FAM_MEANING_CLICK_VALUE;
            }
            else if (radMeaning.Checked)
            {
                lblKanji.Text = curDtoVoc.Meaning;
                lblHiragana.Text = Constants.RES_FAM_HIRAGANA_CLICK_VALUE;
                lblMeaning.Text = Constants.RES_FAM_KANJI_CLICK_VALUE;
            }

            lblStatus.Text = (curIndex + 1).ToString() + "/" + total.ToString();
        }

        private void GetVocToDisplay()
        {
            if (isRandom)
            {
                if (curIndex + 1 == viewedIndex)
                {
                    Random rd = new Random();
                    int index = rd.Next(0, arrVocs.Count - 1);
                    curDtoVoc = (DTO_VOC)arrVocs[index];

                    arrViewedVocs.Add(new DTO_VOC(curDtoVoc));
                    arrVocs.RemoveAt(index);

                    viewedIndex++;
                    curIndex++;
                }
                else
                {
                    curIndex++;
                    curDtoVoc = (DTO_VOC)arrViewedVocs[curIndex];
                }
            }
            else
            {
                curIndex++;
                curDtoVoc = (DTO_VOC)arrVocs[curIndex];
            }
        }

        private void GetPreviousVocToDisplay()
        {
            if (isRandom)
            {
                curIndex--;
                curDtoVoc = (DTO_VOC)arrViewedVocs[curIndex];
            }
            else
            {
                curIndex--;
                curDtoVoc = (DTO_VOC)arrVocs[curIndex];
            }
        }

        private void UpdateFont()
        {
            if (!radAll.Checked)
            {
                lblHiragana.Font = Common.ChangeFontSize(lblHiragana.Font, 10);
                lblMeaning.Font = Common.ChangeFontSize(lblMeaning.Font, 10);
            }
            else
            {
                lblHiragana.Font = Common.ChangeFontSize(lblHiragana.Font, dtoSetting.Hiragana_Fontsize);
                lblMeaning.Font = Common.ChangeFontSize(lblMeaning.Font, dtoSetting.Meaning_Fontsize);
            }
        }
        #endregion

        #region Paint Ellipsis Text of Label
        private void Ellipsis_Label_Paint(object sender, PaintEventArgs e)
        {
            Common.DrawEllipsisText((Label)sender, e.Graphics);
        }
        #endregion

        #region Thao tác trên form
        private void btnNext_Click(object sender, EventArgs e)
        {
            UpdateFont();
            GetVocToDisplay();
            DisplayCurVocs();

            if (curIndex + 1 == total)
            {
                btnNext.Enabled = false;
            }

            if (curIndex > 0)
            {
                btnPrevious.Enabled = true;
            }
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            UpdateFont();
            GetPreviousVocToDisplay();
            DisplayCurVocs();

            if (curIndex == 0)
            {
                btnPrevious.Enabled = false;
            }

            if (curIndex + 1 == total)
            {
                btnNext.Enabled = false;
            }
            else
            {
                btnNext.Enabled = true;
            }
        }

        private void Checkbox_CheckedChanged(object sender, EventArgs e)
        {
            if (radAll.Checked || radKanji.Checked)
            {
                //Kanji
                pnlKanji.BackColor = Color.FromArgb(dtoSetting.Kanji_BackColor);
                pnlKanji.ForeColor = Color.FromArgb(dtoSetting.Kanji_FontColor);
                lblKanji.Font = Common.ChangeFontSize(lblKanji.Font, dtoSetting.Kanji_Fontsize);
                
                // Meaning
                pnlMeaning.BackColor = Color.FromArgb(dtoSetting.Meaning_BackColor);
                pnlMeaning.ForeColor = Color.FromArgb(dtoSetting.Meaning_FontColor);
            }
            else
            {
                //Kanji
                pnlKanji.BackColor = Color.FromArgb(dtoSetting.Meaning_BackColor);
                pnlKanji.ForeColor = Color.FromArgb(dtoSetting.Meaning_FontColor);
                lblKanji.Font = Common.ChangeFontSize(lblKanji.Font, dtoSetting.Meaning_Fontsize);

                // Meaning
                pnlMeaning.BackColor = Color.FromArgb(dtoSetting.Kanji_BackColor);
                pnlMeaning.ForeColor = Color.FromArgb(dtoSetting.Kanji_FontColor);
            }

            UpdateFont();

            DisplayCurVocs();
        }

        private void lblHiragana_Click(object sender, EventArgs e)
        {
            if (!radAll.Checked)
            {
                if (lblHiragana.Text.Equals(Constants.RES_FAM_HIRAGANA_CLICK_VALUE))
                {
                    lblHiragana.Font = Common.ChangeFontSize(lblHiragana.Font, dtoSetting.Hiragana_Fontsize);
                    lblHiragana.Text = curDtoVoc.Hiragana;
                }
                else
                {
                    lblHiragana.Font = Common.ChangeFontSize(lblHiragana.Font, 10);
                    lblHiragana.Text = Constants.RES_FAM_HIRAGANA_CLICK_VALUE;
                }
            }
        }

        private void lblMeaning_Click(object sender, EventArgs e)
        {
            if (radKanji.Checked)
            {
                if (lblMeaning.Text.Equals(Constants.RES_FAM_MEANING_CLICK_VALUE))
                {
                    lblMeaning.Font = Common.ChangeFontSize(lblMeaning.Font, dtoSetting.Meaning_Fontsize);
                    lblMeaning.Text = curDtoVoc.Meaning;
                }
                else
                {
                    lblMeaning.Font = Common.ChangeFontSize(lblMeaning.Font, 10);
                    lblMeaning.Text = Constants.RES_FAM_MEANING_CLICK_VALUE;
                }
            }
            else if (radMeaning.Checked)
            {
                if (lblMeaning.Text.Equals(Constants.RES_FAM_KANJI_CLICK_VALUE))
                {
                    lblMeaning.Font = Common.ChangeFontSize(lblKanji.Font, dtoSetting.Kanji_Fontsize);
                    lblMeaning.Text = curDtoVoc.Kanji;
                }
                else
                {
                    lblMeaning.Font = Common.ChangeFontSize(lblMeaning.Font, 10);
                    lblMeaning.Text = Constants.RES_FAM_KANJI_CLICK_VALUE;
                }
            }
        }

        private void lblRefresh_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            viewedIndex = 0;
            curIndex = -1;

            if (isRandom)
            {
                arrVocs.AddRange(arrViewedVocs.ToArray());
                arrViewedVocs.Clear();
            }

            UpdateFont();
            GetVocToDisplay();
            DisplayCurVocs();

            if (total > 1)
            {
                btnNext.Enabled = true;
            }
            else
            {
                btnNext.Enabled = false;
            }

            btnPrevious.Enabled = false;
        }

        private void lblEdit_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            fEditVoc editVoc = new fEditVoc(curDtoVoc, dbPath);
            if (editVoc.ShowDialog() == DialogResult.OK)
            {
                curDtoVoc = editVoc.UpdateDtoVoc;

                if (isRandom)
                {
                    arrViewedVocs[curIndex] = curDtoVoc;
                }
                else
                {
                    arrVocs[curIndex] = curDtoVoc;
                }

                UpdateFont();
                DisplayCurVocs();
            }
        }
        #endregion

        #region Closing
        private void fFamVoc_FormClosing(object sender, FormClosingEventArgs e)
        {
            frmParent.ReShow();
        }
        #endregion
    }
}